package net.newsmth.smthapp.android.ui;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentActivity;
import android.app.ActionBar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


import net.newsmth.smthapp.android.adapter.MainPagerAdapter;
import net.newsmth.smthapp.android.R;


public class MainActivity extends FragmentActivity {

    private ViewPager pager;
    private MainPagerAdapter adapter;
    private ActionBar bar;
    private String[] tabNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources res = getResources();
        tabNames = new String[] {res.getString(R.string.main_tab_hot),
                res.getString(R.string.main_tab_favourite),
                res.getString(R.string.mian_tab_message)};

        pager = (ViewPager) findViewById(R.id.main_pager);
        adapter = new MainPagerAdapter(getSupportFragmentManager());
        adapter.setTabNames(tabNames);
        pager.setAdapter(adapter);
        bar = getActionBar();

        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                bar.setSelectedNavigationItem(position);
            }
        });

        // Don't destroy the fragment through it's slide off
        pager.setOffscreenPageLimit(2);

        InitTabs(bar);
    }

    private void InitTabs(ActionBar bar) {
        ActionBar.TabListener listener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                pager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

            }
        };

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        bar.addTab(bar.newTab().setText(R.string.main_tab_hot).setTabListener(listener));
        bar.addTab(bar.newTab().setText(R.string.main_tab_favourite).setTabListener(listener));
        bar.addTab(bar.newTab().setText(R.string.mian_tab_message).setTabListener(listener));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void testButtonClick(View view) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

}
